#!/usr/bin/env node
import path from 'path';
import fs from 'fs';
import { packageCore } from '../src/package-core';
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import process from 'node:process';

// 初始化包结构
packageCore.initializePackage();

// 迁移目录映射
const migrationMap = {
  // 核心代码
  'src/core': 'core',
  'src/adapters': 'adapters',
  'src/platform': 'platform',
  
  // 测试代码
  'test/core': 'core',
  'test/adapters': 'adapters',
  
  // 配置
  '.vscode': 'config/vscode',
  '.config': 'config',
  
  // 资源
  'proto': 'proto',
  'wasm': 'wasm'
};

// 执行迁移
async function runMigration() {
  console.log('Starting package migration...');
  
  try {
    for (const [source, dest] of Object.entries(migrationMap)) {
      const sourcePath = path.join(process.cwd(), source);
      if (fs.existsSync(sourcePath)) {
        console.log(`Migrating ${source}...`);
        await packageCore.migrateToPackage(sourcePath, dest);
      }
    }
    
    console.log('Migration completed successfully!');
    console.log('Package structure initialized at: pkg/');
  } catch (error) {
    if (error instanceof Error) {
      console.error('Migration failed:', error.message);
    } else {
      console.error('Migration failed:', String(error));
    }
    process.exit(1);
  }
}

runMigration();